<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
    <link rel="shortcut icon" href="log.png">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="plugins/element-ui/index.css"/>


    <link rel="stylesheet" href="styles/login.css"/>
    <script src="js/validate.js"></script>

</head>
<body>
<div class="app" id="app">
    <div class="ui-login">
        <div class="mask_body_item change_login">
            <span type="accountlogin" class="account_item active">账号密码登录</span>
            <span type="mobilelogin" class="phonenum_item">手机动态码登录</span>
        </div>
        <div class="ui-form">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                     class="demo-ruleForm">
                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="ruleForm.phone"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="plugins/axios/axios.min.js"></script>
<script src="js/request.js"></script>
<script>
    new Vue({
        el: '#app',

        data() {
            function isCellPhone(val) {
                if (!/^1(3|4|5|6|7|8)\d{9}$/.test(val)) {
                    return false
                } else {
                    return true
                }
            }

            checkPhone = (rule, value, callback) => {
                // let phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/;
                if (value == "") {
                    callback(new Error("请输入手机号"))
                } else if (!isCellPhone(value)) {//引入methods中封装的检查手机格式的方法
                    callback(new Error("请输入正确的手机号!"))
                } else {
                    callback()
                }
            };
            validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    phone: '',
                    pass: '',
                    checkPass: ''
                },
                rules: {
                    phone: [
                        {validator: checkPhone, trigger: 'blur'}
                    ],
                    pass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    });
</script>
</body>

</html>